<?php
session_start(); //开启session会话 类似于sessionStorage
if ($_GET) {
    if (isset($_GET['w']) && isset($_GET['h']) && isset($_GET['fs'])) {
        $w = $_GET['w'];
        $h = $_GET['h'];
        $fs = $_GET['fs'];
        $image = imagecreatetruecolor($w, $h);
        $backcolor = imagecolorallocate($image, 255, 255, 255); //生成颜色
        $bordercolor = imagecolorallocate($image, 0, 255, 0); //生成颜色
//$textcolor = imagecolorallocate($image, 0, 0, 0); //生成颜色

        imagefilledrectangle($image, 0, 0, $w, $h, $backcolor); //绘制背景颜色

//imagerectangle($image, 0, 0, $w-1, $h-1, $bordercolor); //绘制边框

        $codestr = 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789';

//绘制文字内容
        $testStr = '';
        for ($i = 0; $i < 4; $i++) {
            $index = mt_rand(0, strlen($codestr) - 1);
            $text = substr($codestr, $index, 1);
            $testStr .= $text;
            $textcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //生成颜色
            imagefttext($image, $fs, mt_rand(-10, 10), $fs * 0.8 * (1 + $i) - 4, $h / 2 + $fs / 2, $textcolor, 'c://WINDOWS//Fonts//simsun.ttc', $text);
        }
//绘制线段
        for ($i = 5; $i--;) {
            $lineColor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            imageline($image, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $h), $lineColor);
        }
//绘制像素点
        for ($i = 50; $i--;) {
            $pointColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($image, mt_rand(0, $w), mt_rand(0, $h), $pointColor);
        }

        header('Cache-Control:max-age=1,s-maxage=1,no-cache,must-revalidate');
        header('Content-type:image/png;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        include_once 'dbConfig.php';
        $sql = 'UPDATE lgz_validate SET session_val = "' . $testStr . '" WHERE id = 1';
        if ($link->query($sql)) {
            imagepng($image); //生成png图片
            imagedestroy($image); //销毁画布
        }
    }
}

